package workorder

import (
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/dao"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/entity"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/global"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/model"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	"strconv"
	"time"
)

//********************************************
//Func  ：添加工单流转记录
//Method：POST
//Usage 接口调用
//********************************************
func InsertFlow(c *gin.Context) {
	var flow entity.WorkOrderFlow
	var err error
	flow.ProcessId, err = strconv.ParseInt(c.DefaultQuery("processId", ""), 10, 64)
	if err != nil {
		global.Log.Error("InsertFlow processId is lost.")
		c.JSON(http.StatusOK, model.Fail("未指明选用的流程"))
		return
	}

	flow.StepId, err = strconv.ParseInt(c.DefaultQuery("stepId", ""), 10, 64)
	if err != nil {
		global.Log.Error("InsertFlow stepId is lost.")
		c.JSON(http.StatusOK, model.Fail("未指明流程中选用步骤"))
		return
	}

	flow.WorkOrderId, err = strconv.ParseInt(c.DefaultQuery("workOrderId", ""), 10, 64)
	if err != nil {
		global.Log.Error("InsertFlow stepId is lost.")
		c.JSON(http.StatusOK, model.Fail("未指明工单"))
		return
	}

	flow.PrevFlowId, err = strconv.ParseInt(c.DefaultQuery("prevFlowId", "0"), 10, 64)
	if err != nil {
		flow.WorkOrderId = 0 //未指明则说明是第一步，灭有上级
	}

	status, err := strconv.Atoi(c.DefaultPostForm("workOrderStatus", "0"))
	if err != nil {
		global.Log.Error("parse workOrderStatus failed.", zap.Any("error", err))
	}
	flow.WorkOrderStatus = entity.EWorkerOrderStatus(status)

	newflow, err := dao.InsertFlow(flow) //
	if err != nil {
		global.Log.Error("InsertFlow failed.", zap.Any("error", err))
		c.JSON(http.StatusOK, model.Fail("添加流转记录失败"))
	} else {
		c.JSON(http.StatusOK, model.SuccessObject("操作成功", newflow))
		global.Log.Info("InsertFlow success.", zap.Any("newflow", newflow))
	}
}

//********************************************
//Func  ：添加工单流转记录
//Method：POST
//Usage  函数调用。功能与InsertFlow相同，但是本函数是由其它后端接口/函数进行调用的，接口是由其它前端界面发起调用的。
//********************************************
func InsertFlowFunc(workOrderId, processId, stepId, prevFlowId int64, status entity.EWorkerOrderStatus) (*entity.WorkOrderFlow, error) {
	var flow entity.WorkOrderFlow
	var err error
	flow.ProcessId = processId
	flow.StepId = stepId
	flow.WorkOrderId = workOrderId
	flow.PrevFlowId = prevFlowId
	flow.WorkOrderStatus = status
	newflow, err := dao.InsertFlow(flow) //
	return newflow, err
}

//********************************************
//Func  ：更新工单流为结束(流转.人工)
//Method：POST
//Usage:
//input 下一步流程ID，
//********************************************
func UpdateFlowToEnd(c *gin.Context) {
	var flow entity.WorkOrderFlow
	var err error
	flow.ProcessId, err = strconv.ParseInt(c.DefaultQuery("processId", ""), 10, 64)
	if err != nil {
		global.Log.Error("InsertFlow stepId is lost.")
		c.JSON(http.StatusOK, model.Fail("未指明选用的流程"))
		return
	}

	flow.StepId, err = strconv.ParseInt(c.DefaultQuery("stepId", ""), 10, 64)
	if err != nil {
		global.Log.Error("InsertFlow stepId is lost.")
		c.JSON(http.StatusOK, model.Fail("未指明流程中选用步骤"))
		return
	}

	flow.WorkOrderId, err = strconv.ParseInt(c.DefaultQuery("workOrderId", ""), 10, 64)
	if err != nil {
		global.Log.Error("InsertFlow stepId is lost.")
		c.JSON(http.StatusOK, model.Fail("未指明工单"))
		return
	}

	flow.NextFlowId, err = strconv.ParseInt(c.DefaultQuery("nextFlowId", "0"), 10, 64)

	status, err := strconv.Atoi(c.DefaultPostForm("workOrderStatus", "0"))
	if err != nil {
		global.Log.Error("parse workOrderStatus failed.", zap.Any("error", err))
	}
	flow.WorkOrderStatus = entity.EWorkerOrderStatus(status)
	flow.EndAt = time.Now()

	newflow, err := dao.UpdateFlow(flow, "next_flow_id,end_at")
	if err != nil {
		global.Log.Error("InsertFlow failed.", zap.Any("error", err))
		c.JSON(http.StatusOK, model.Fail("添加流转记录失败"))
	} else {
		c.JSON(http.StatusOK, model.SuccessObject("操作成功", newflow))
		global.Log.Info("InsertFlow success.", zap.Any("newflow", newflow))
	}
}

//********************************************
//Func  ：更新工单流为结束(流转.人工)
//Method：POST
//Usage:
//input 函数调用。功能与InsertFlow相同，但是本函数是由其它后端接口/函数进行调用的，接口是由其它前端界面发起调用的。，
//********************************************
func UpdateFlowToEndFunc(workOrderId, processId, stepId, nextFlowId int64, status entity.EWorkerOrderStatus) (*entity.WorkOrderFlow, error) {
	var flow entity.WorkOrderFlow
	var err error
	flow.ProcessId = processId
	flow.StepId = stepId
	flow.WorkOrderId = workOrderId
	flow.NextFlowId = nextFlowId
	flow.WorkOrderStatus = status

	flow.EndAt = time.Now()
	newflow, err := dao.UpdateFlow(flow, "next_flow_id,end_at")
	return newflow, err
}

//********************************************
//Func  ：查询工单的流转列表
//Method：GET
//Usage ：
//********************************************
func QueryFlowOfOrder(c *gin.Context) {
	pageSize, err1 := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
	if err1 != nil {
		global.Log.Error("QueryFlowOfOrder input param pagesize error:", zap.Any("error", err1))
		c.JSON(http.StatusOK, model.Fail("请求参数错误"))
		return
	}

	page, err2 := strconv.Atoi(c.DefaultQuery("page", "1"))
	if err2 != nil {
		global.Log.Error("QueryFlowOfOrder input param page error:", zap.Any("error", err2))
		c.JSON(http.StatusOK, model.Fail("请求参数错误"))
		return
	}
	orderId, err := strconv.ParseInt(c.DefaultQuery("workOrderId", ""), 10, 64)
	if err != nil {
		global.Log.Error("QueryFlowOfOrder orderId is lost.")
		c.JSON(http.StatusOK, model.Fail("未指明查询工单"))
		return
	}
	if orderId <= 0 {
		global.Log.Error("QueryDepartmentDetail orderId is lost.")
		c.JSON(http.StatusOK, model.Fail("未指明查询工单"))
		return
	}

	total, flows, err := dao.QueryFlowList(page, pageSize, orderId)

	type WorkOrderFlow struct {
		Total int64
		List  []*entity.WorkOrderFlow
	}
	output := &WorkOrderFlow{
		Total: total,
		List:  flows,
	}
	c.JSON(http.StatusOK, model.SuccessObject("操作成功", output))
	global.Log.Info("QueryAllDepartments success.", zap.Any("flow list", output))
}
